package com.hualan.sfzy.controller.psyc;

import com.hualan.sfzy.entity.PsycTestOption;
import com.hualan.sfzy.service.psyc.PsycTestOptionService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class PsycTestOptionController {
    @Autowired
    private PsycTestOptionService topicChooseService;
    /**
     * 批量新增选项
     * @param topicChooses 选项列表
     * @return 操作结果
     */
    @PostMapping("/psycTestOption/batch")
    public Result batchInsert(@RequestBody List<PsycTestOption> topicChooses) {
        int count = topicChooseService.insertBatch(topicChooses);
        return count > 0 ? Result.success() : Result.error();
    }
    @PostMapping("/psycTestOption")
    public String add(@RequestBody PsycTestOption topicChoose){
        return topicChooseService.add(topicChoose)>0?"新增成功":"新增失败";
    }
    @DeleteMapping("/psycTestOption/{id}")
    public String removeById(@PathVariable Integer id){
        return topicChooseService.remove(id)>0?"删除成功":"删除失败";
    }
    // 根据题目ID删除所有选项
    @DeleteMapping("psycTestOption/byTopic/{topicId}")
    public Result deleteByTopicId(@PathVariable Integer topicId) {
        int count = topicChooseService.deleteBatchByTestTitleId(topicId);
        if (count > 0) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败或记录不存在");
        }
    }
    @DeleteMapping("/psycTestOption")
    public String remove(Integer[] ids){
        return topicChooseService.removeBatch(ids)>0?"批量删除成功":"批量删除失败";
    }
    @PutMapping("/psycTestOption")
    public String edit(@RequestBody PsycTestOption topicChoose){
        return topicChooseService.edit(topicChoose)>0?"修改成功":"修改失败";
    }
    @GetMapping("/psycTestOption/{id}")
    public PsycTestOption findById(@PathVariable Integer id){
        return topicChooseService.findById(id);
    }
/*    @GetMapping("/topicChoose")
    List<TopicChoose> find(){
        return topicChooseService.find();
    }*/

    /*@GetMapping("/topicChoose")
    public List<TopicChoose> find(
            @RequestParam(value = "topicId", required = false) Integer topicId,
            @RequestParam(value = "del", required = false) Integer del
    ) {
        TopicChoose topicChoose = new TopicChoose();
        topicChoose.setTopicId(topicId);
        topicChoose.setDel(del);
        return topicChooseService.find(topicChoose);
    }*/

    @GetMapping("/psycTestOption")
    public List<PsycTestOption> find(
            @RequestParam(value = "topicId", required = false) Integer topicId,
            @RequestParam(value = "del", required = false) Integer del
    ) {
        PsycTestOption topicChoose = new PsycTestOption();
        topicChoose.setTestTitleId(topicId);
        topicChoose.setDel(del);
        return topicChooseService.findParams(topicChoose);
    }

    @GetMapping("/psycTestOption/pages")
    Map<String,Object> findPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                                PsycTestOption topicChoose){
        return topicChooseService.findPage(pageNum,pageSize,topicChoose);
    }
}